﻿
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using EDMXParserV1;
using System.Windows.Forms;

namespace MVCGenerator
{
    public static class Basics
    {


     

        public static String MakeFriendlyDisplayField(this String Expression)
        {
            String Result = Expression.SpaceAfterCapitalLetters();

            if (Result.EndsWith(" I D"))
            {
                Result = Result.Substring(0, Result.Length - 2) + "D";
            }

            return Result;
        }

        public static String SpaceAfterCapitalLetters(this String Expression)
        {
            string output = "";

            foreach (char letter in Expression)
            {
                if (Char.IsUpper(letter) && output.Length > 0)
                    output += " " + letter;
                else
                    output += letter.ToString();
            }
            return output;
        }

        public static int ToInt32(this String Expression)
        {
            return Convert.ToInt32(Expression);
        }

        public static int ToInt32(this Object Expression)
        {
            return Convert.ToInt32(Expression.ToString());
        }

      

        // a little overreacting.
        public static String ToString(this DataGridViewCell Expression)
        {
            if (Expression.Value == null)
                return "";
            return Expression.Value.ToString();
        }

        public static bool IsNumericType(String TypeName)
        {
            TypeName = TypeName.ToLower();
            
            if (TypeName.Contains("int64"))
                return true; 
            
            if (TypeName.Contains("int32"))
                return true;

            if (TypeName.Contains("int16"))
                return true;

            if (TypeName.Contains("decimal"))
                return true;

            if (TypeName.Contains("short"))
                return true;

            if (TypeName.Contains("single"))
                return true;

            return false;

        }

        public static String GetDataTypeFromTableNameColumnName(String TableName, String ColumnName, EntitySet SelectedEntitySet)
        {
            foreach (NavigationProperty Item in SelectedEntitySet.EntityTypeLink.NavigationProperties)
            {
                if (TableName == Item.Name)
                {
                    foreach (EDMXParserV1.Property Property in Item.ToEntityType.Properties)
                    {
                        if (Property.Name == ColumnName)
                        {
                            return Property.Type;
                        }
                    }
                }
            }
            return "";
        }


        public static String GetClassDetailClassBaseClass(string PropertyClassName, List<GridChoice> GridChoices, EntitySet SelecteEntitySet)
        {
            SpaceStackStringBuilder sb = new SpaceStackStringBuilder();
            sb.AddSpace();
            sb.AppendLine();
            sb.AppendLine("// Model when displaying in a list");
            sb.AppendLine("public class " + PropertyClassName + "Detail : " + PropertyClassName + "Base");
            sb.AppendLine("{");
            if (true)
            {
                sb.AddSpace();

                foreach (var item in GridChoices)
                {
                    sb.AppendLine();
                    String DataType = Basics.GetDataTypeFromTableNameColumnName(item.PrimaryTable, item.DisplayColumnName, SelecteEntitySet);

                    sb.AppendLine(@"[Display( Name=""" + (item.PrimaryTable + item.DisplayColumnName).MakeFriendlyDisplayField() + @""")]");
                    sb.AppendStart();
                    sb.Append("public " + DataType);
                    if (item.CanLinkedTableBeNull)
                    {
                        if (DataType == "String")
                        {
                            // do nothing
                        }
                        else
                        {
                            sb.Append("?");
                        }
                    }
                    sb.Append(" " + item.PrimaryTable + item.DisplayColumnName + " { get; set; }");
                    sb.AppendLine();
                }

                if (GridChoices.Count == 0)
                {
                    sb.AppendLine("// There were not cross reference tables otherwise the Display field(s) would display.");
                }
                sb.RemoveSpace();
            }
            sb.AppendLine("}");
            return sb.ToString();
        }


    }
}